package com._bytecode.__asm._ClassWriter;

import org.objectweb.asm.ClassWriter;
import sun.applet.AppletClassLoader;

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import static org.objectweb.asm.Opcodes.*;

/**
 * @Author: huangya
 * @Date: 2020/09/16
 * @Description:
 *********************************************************
 * package com._bytecode.__asm;
 *
 * public interface Comparable extends Mesurable {
 *      int LESS = -1;
 *      int EQUAL = 0;
 *      int GREATER = 1;
 *      int compareTo(Object o);
 * }
 * *******************************************************
 */
public class GeneratingClassTest {

    public static void main(String[] args) throws IOException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        ClassWriter cw = new ClassWriter(0);
        cw.visit(V1_5, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE,
                "com/_bytecode/__asm/Comparable", null, "java/lang/Object",
                new String[]{"com/_bytecode/__asm/Mesurable"});
        cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "LESS", "I",
                null, new Integer(-1)).visitEnd();
        cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "EQUAL", "I",
                null, new Integer(0)).visitEnd();
        cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "GREATER", "I",
                null, new Integer(1)).visitEnd();
        cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "compareTo",
                "(Ljava/lang/Object;)I", null, null).visitEnd();
        cw.visitEnd();
        byte[] b = cw.toByteArray();

        /** 加载class */
        MyClassLoader myClassLoader = new MyClassLoader();
        Class c = myClassLoader.defineClass("com._bytecode.__asm.Comparable", b);
        //Object compareTo = c.getDeclaredMethod("compareTo", Object.class).invoke(c.newInstance(), 1);
    }
}
